package com.hxzy.servlet.TbUserFromId;

import com.hxzy.entity.TbUserFromId;
import com.hxzy.service.TbUserFromIdService;
import com.hxzy.service.impl.TbUserFromIdServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/*
 *  根据主键查询信息 显示到网页上
 */
@WebServlet(urlPatterns = "/TbUserFromId/update")
public class update extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
        String id = req.getParameter("id");//接收网页传入数据

        //查询数据
        TbUserFromIdService service=new TbUserFromIdServiceImpl();
        TbUserFromId tbUserFromId = service.selectByPrimaryKey(Long.parseLong(id));

        /*
         * 将数据打包并且转发    并给予地址
         * */
        req.setAttribute("id",tbUserFromId);
        req.getRequestDispatcher("/WEB-INF/TbUserFromId/update.jsp").forward(req,res);
    }
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        res.setCharacterEncoding("UTF-8");
        res.setContentType("text/html;charset=UTF-8");

        TbUserFromId tbUserFromId=new TbUserFromId();

        /**
         * 用户id
         */
        Long userId=Long.parseLong(req.getParameter("userId"));
        tbUserFromId.setUserId(userId);

        /**
         * 小程序推送formId
         */
        String formId=req.getParameter("formId");
        tbUserFromId.setFormId(formId);

        /**
         * 来源 source 1，步数兑换海贝按钮；2，首页邀请按钮；3，步数拦截弹窗邀请按钮；4，兑换商品按钮；\n\n5，海贝不够邀请按钮；6，引导关注蒙层按钮；7，健康体验领取按钮 8，首页弹窗“马上领取”按钮
         */
        Short source=Short.parseShort(req.getParameter("source"));
        tbUserFromId.setSource(source);

        /**
         * 使用状态 1 未使用[默认] 2 已使用
         */
        Short status=Short.parseShort(req.getParameter("status"));
        tbUserFromId.setStatus(status);

        /**
         * 创建时间
         */
        SimpleDateFormat sdf=new SimpleDateFormat("YYYY-MM-DD");
        try {
            Date createTime=sdf.parse(req.getParameter("createTime"));
            tbUserFromId.setCreateTime(createTime);
        } catch (ParseException e) {
            e.printStackTrace();
        }

        /**
         * 修改时间
         */
        try {
            Date updateTime=sdf.parse(req.getParameter("updateTime"));
            tbUserFromId.setUpdateTime(updateTime);
        } catch (ParseException e) {
            e.printStackTrace();
        }

        Long id=Long.parseLong(req.getParameter("id"));
        tbUserFromId.setId(id);

        TbUserFromIdService tbUserFromIdService=new TbUserFromIdServiceImpl();
        boolean insert = tbUserFromIdService.updateByPrimaryKey(tbUserFromId);


//        PrintWriter out=res.getWriter();
//        out.println( insert? "修改成功": "修改失败");
//        //输出刷新
//        out.flush();
//        out.close();
        String path=req.getContextPath();
        req.setAttribute("url",path+"/TbUserFromId/search");
        //转发
        req.getRequestDispatcher("/WEB-INF/jump.jsp").forward(req,res);
    }
}
